﻿using System;
using System.Collections;
using System.Drawing;
using AMS.Core.Entities;
using VitCode.Castle.Management;

namespace AMS.Web.UserControls
{
    public partial class BarcodeCtl : System.Web.UI.UserControl
    {
        BaseDataManager m_Help = new BaseDataManager();
        private static string strBarcode;
        public string Text
        {
            get { return txtBarcode.Text; }
            set { txtBarcode.Text = value; }
          
        }
        public string ObjClassName
        {
            get { return hdnClassName.Value; }
            set { hdnClassName.Value = value; }
        }

        public string ObjTypeName
        {
            get { return hdnTypeName.Value; }
            set { hdnTypeName.Value = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(txtBarcode.Text.Trim()))
                {

                    if (hdnClassName.Value == "AssetInfo")
                    {
                        if (!string.IsNullOrEmpty(hdnTypeName.Value))
                        {
                            var currentBarcodeFormat = GetBarcodeFormatByAssetTypeName(hdnTypeName.Value);
                            if (currentBarcodeFormat != null)
                            {
                                string strProfix = currentBarcodeFormat.Prefix;
                                bool bIsEnableDate = currentBarcodeFormat.IsEnableDate;

                                if (bIsEnableDate)
                                {

                                    string strFix = strProfix;
                                    strBarcode = m_Help.GetValueRunProc("Assets_barcodeSel", strFix, true);
                                    txtBarcode.Text = strBarcode;
                                }

                                else
                                {

                                    string strFix = strProfix;
                                    strBarcode = m_Help.GetValueRunProc("Assets_barcodeSel", strFix, false);
                                    txtBarcode.Text = strBarcode;
                                }

                            }
                            else
                            {
                                var currentBarcodeFormat2 = GetBarcodeFormatByAssetTypeName("MainAsset");
                                string strProfix = currentBarcodeFormat2.Prefix;
                                bool bIsEnableDate = currentBarcodeFormat2.IsEnableDate;

                                if (bIsEnableDate)
                                {

                                    string strFix = strProfix;
                                    strBarcode = m_Help.GetValueRunProc("Assets_barcodeSel", strFix, true);
                                    txtBarcode.Text = strBarcode;
                                }

                                else
                                {

                                    string strFix = strProfix;
                                    strBarcode = m_Help.GetValueRunProc("Assets_barcodeSel", strFix, false);
                                    txtBarcode.Text = strBarcode;
                                }
                            }
                        }
                    }
                }
                else
                {
                    btnChoce.Visible = false;
                } 
            }
        }

        private BarcodeFormat GetBarcodeFormatByAssetTypeName(string assetTypeName)
        {
            IList objList = m_Help.GetObjectList(new BarcodeFormat().GetType().Name, "AssetTypeName",
                                                           assetTypeName);
            if (objList != null && objList.Count > 0)
            {
                var currentBarcodeFormat = (BarcodeFormat)objList[0];
                return currentBarcodeFormat;
            }
            return null;
        }

        protected void btnChoce_Click(object sender, EventArgs e)
        {
            if(txtBarcode.ReadOnly)
            {
                btnChoce.Text = "自动获取";
                txtBarcode.ReadOnly = false;
                txtBarcode.BackColor = Color.White;
            }
            else
            {
                btnChoce.Text = "手动输入";
                txtBarcode.ReadOnly = true;
                txtBarcode.BackColor = Color.OldLace;
                txtBarcode.Text = strBarcode;
            }
         
        }
    }
}